﻿// See https://aka.ms/new-console-template for more information
using CodingWiki_DataAccess.Data;
using CodingWiki_Model.Models;
using Microsoft.EntityFrameworkCore;

Console.WriteLine("Hello, World!");


//using (ApplicationDbContext context = new())
//{
//    //如果数据库和表存在 不操作，如果 数据库存在 没有表 用于创造数据库表模式，数据库和表不存在 都创建
//    context.Database.EnsureCreated();

//    //获取所有未应用于数据库的所有迁移
//    if (context.Database.GetPendingMigrations().Count() > 0)
//    {
//        //迁移
//        context.Database.Migrate();
//    }
//}


//AddBook();
//GetAllBooks();
//GetBook();
//UpdateBook();
//DeleteBook();

//async void DeleteBook()
//{
//    using var context = new ApplicationDbContext();
//    var book = await context.Books.FindAsync(1003);
//    context.Books.Remove(book);
//    await context.SaveChangesAsync();
//}

//async void UpdateBook()
//{
//    try
//    {
//        using var context = new ApplicationDbContext();
//        var books = await context.Books.Where(u => u.Publisher_Id == 1).ToListAsync();
//        //Console.WriteLine(book.Title + " - " + book.ISBN);
//        foreach (var book in books)
//        {
//            book.Price = 55.55m;
//        }
//        await context.SaveChangesAsync();
//    }
//    catch (Exception e)
//    {

//    }
//}

//async void GetBook()
//{
//    try
//    {
//        using var context = new ApplicationDbContext();
//        //var b=  context.Books.Where(u => EF.Functions.Like(u.ISBN, "12%")).Max Count;   //12开头的数据
//        //OrderBy(u=>u.Title).ThenByDescending(u=>u.ISBN) 先排Title  再 按 ISB排 上到下
//        var books = await context.Books.Skip(0).Take(2).ToListAsync();
//        //Console.WriteLine(book.Title + " - " + book.ISBN);
//        foreach (var book in books)
//        {
//            Console.WriteLine(book.Title + " - " + book.ISBN);
//        }

//        books = await context.Books.Skip(4).Take(1).ToListAsync();
//        foreach (var book in books)
//        {
//            Console.WriteLine(book.Title + " - " + book.ISBN);
//        }
//    }
//    catch (Exception e)
//    {

//    }
//}

//void GetAllBooks()
//{
//    using var context = new ApplicationDbContext();
//    var books = context.Books.ToList();
//    foreach (var book in books)
//    {
//        Console.WriteLine(book.Title + "-" + book.ISBN);
//    }
//}

//void AddBook()
//{
//    Book book = new() { Title = "New EF Core Book", ISBN = "9999", Price = 10.93m, Publisher_Id=1 };

//    using var context = new ApplicationDbContext();
//    var books = context.Books.Add(book);
//    context.SaveChanges();
//}